#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
using namespace std;
class Solution
{
public:
    int maxScore(vector<int> &cardPoints, int k)
    {
        vector<int> csum(cardPoints.begin(), cardPoints.end());
        for (int i = 1; i < csum.size(); ++i)
            csum[i] += csum[i - 1];
        int i = 0, j = cardPoints.size() - k - 1, ans = 0, p = cardPoints.size() - 1; // [i,j)
        if (j < 0)
            return csum[p];
        for (; j < cardPoints.size(); ++j, ++i)
        {
            ans = max(ans, csum[i] + csum[p] - csum[j] - cardPoints[i]);
        }
        return ans;
    }
};